/**
 * 
 */
package javacommon.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.tags.TableColumn;
import org.htmlparser.util.NodeList;

/**
 * @author Nail
 * 
 */
public class ParseHTML {

	public static void main(String[] args) {
		for(int i=1;i<72;i++){
			String filePath = "http://www.xbooking.com/activity_search.php?category=tennis&province=0&city=0&gymname=&Input=%CB%D1%CB%F7%B3%A1%B5%D8&page=2&page="+i;
			//parse(filePath);
		}
		
	}

	/*private static void parseHTML(String url) {
		try {
			Document document = getDocumentInstance(url);
			Element root = document.getDocumentElement();
			root.getElementsByTagName("table");
			NodeList nodeList = root.getElementsByTagName("form").item(0).getChildNodes();
			for (int i = 0; i < nodeList.getLength(); i++) {
				Node subNode = nodeList.item(i);
				if (subNode.getNodeType() == Node.ELEMENT_NODE) {
					Element eNode = (Element) subNode;
					if (eNode.getTagName().equals("input")) {
						String name = subNode.getAttributes().getNamedItem("name").getNodeValue().toString();
						String value = subNode.getAttributes().getNamedItem("value").getNodeValue().toString();
						System.out.println("name=" + name + "  value=" + value);
					}
				}
			}
		} catch (Exception e) {
			System.out.println("errMsg: " + e.getMessage());
		}
	}

	private static Document getDocumentInstance(String url) throws Exception {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder db = factory.newDocumentBuilder();
		return db.parse(ConnectURL.getContent(url));
	}*/
	
	
	public List parse(String url) { 
		
		List list = new ArrayList();
        try {   
            /* 获取所有<table id="tblSongList">的tag */  
            Parser parser = new Parser();   
            parser.setEncoding("utf-8");   
            parser.setURL(url);   
            AndFilter filter = new AndFilter(new NodeFilter[] {   
                    new TagNameFilter("table"),   
                    new HasAttributeFilter("class", "s_table") });   
            NodeList nodes = parser.parse(filter);  
            AndFilter trFilter = new AndFilter(new NodeFilter[] {   
                    new TagNameFilter("tr") }); 
            NodeList trList = nodes.extractAllNodesThatMatch(trFilter,true);
            //nodes.elements();
            NodeFilter  tdFilter = new TagNameFilter("td");
           
            //System.out.println(trList.size());
            for(int i=1;i<trList.size();i++){
            	Map map = new HashMap();
            	Node node = trList.elementAt(i);
            	NodeList tdList = node.getChildren().extractAllNodesThatMatch(tdFilter,true);//.extractAllNodesThatMatch(tdFilter, true);
            	//for(int j=0;j<tdList.size();j++){
            		String link = ((LinkTag)tdList.elementAt(0).getFirstChild()).getLink();
            		//System.out.println(link);
            		Parser parser2 = new Parser();   
                    parser2.setEncoding("utf-8");   
                    parser2.setURL(link+"&action=description");
                    filter = new AndFilter(new NodeFilter[] {   
                            new TagNameFilter("td"),   
                            new HasAttributeFilter("class", "content_001"),
                            new HasAttributeFilter("height", "32")});  
                    NodeList tempNodes = parser2.parse(filter);
                    //tempNodes.toHtml();
            		//System.out.println(((TableColumn)tempNodes.elementAt(0)).getChildrenHTML());
            		Object object[] =new Object[5];
            		map.put("name", tdList.elementAt(0).toPlainTextString());
            		map.put("address", tdList.elementAt(2).toPlainTextString());
            		map.put("count", tdList.elementAt(3).toPlainTextString());
            		map.put("city", tdList.elementAt(5).toPlainTextString());
            		map.put("desc", ((TableColumn)tempNodes.elementAt(0)).toPlainTextString());
            			object[0] =tdList.elementAt(0).toPlainTextString();
            			object[1] =tdList.elementAt(2).toPlainTextString();
            			object[2] =tdList.elementAt(3).toPlainTextString();
            			object[3] =tdList.elementAt(5).toPlainTextString();
            			object[4] =((TableColumn)tempNodes.elementAt(0)).toPlainTextString();
            	//}
            		
            		list.add(object);
            }
            
        } catch (Exception e) {   
            e.printStackTrace();   
        }   
        return list;
    } 

}
